home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Examples / Helpfile / GUICtrlCreateContextMenu.au3 < prev    next >
Text File  |  2007-09-08  |  4KB  |  124 lines

  1. ; ****************
  2. ; * First sample *
  3. ; ****************
  4.  
  5. #include <GUIConstants.au3>
  6.  
  7. ;right click on gui to bring up context Menu.
  8. ;right click on the "ok" button to bring up a controll specific context menu.
  9.  
  10. GUICreate("My GUI Context Menu", 300, 200)
  11.  
  12. $contextmenu    = GUICtrlCreateContextMenu ()
  13.  
  14. $button            = GUICtrlCreateButton("OK", 100, 100, 70, 20)
  15. $buttoncontext    = GUICtrlCreateContextMenu($button)
  16. $buttonitem        = GUICtrlCreateMenuitem("About button", $buttoncontext)
  17.  
  18. $newsubmenu        = GUICtrlCreateMenu ("new", $contextmenu)
  19. $textitem        = GUICtrlCreateMenuitem ("text", $newsubmenu)
  20.  
  21. $fileitem        = GUICtrlCreateMenuitem ("Open", $contextmenu)
  22. $saveitem        = GUICtrlCreateMenuitem ("Save", $contextmenu)
  23. GUICtrlCreateMenuitem ("", $contextmenu)    ; separator
  24.  
  25. $infoitem        = GUICtrlCreateMenuitem ("Info", $contextmenu)
  26.  
  27. GUISetState ()
  28.  
  29. ; Run the GUI until the dialog is closed
  30. While 1
  31.     $msg = GUIGetMsg()
  32.     
  33.     If $msg = $GUI_EVENT_CLOSE Then ExitLoop
  34. Wend
  35.  
  36. Exit
  37.  
  38.  
  39. ; *****************
  40. ; * Second sample *
  41. ; *****************
  42.  
  43. #include <GUIConstants.au3>
  44.  
  45. $hGui            = GUICreate("My GUI", 170, 40)
  46.  
  47. $OptionsBtn        = GUICtrlCreateButton("&Options", 10, 10, 70, 20, $BS_FLAT)
  48.  
  49. ; At first create a dummy control for the options and a contextmenu for it
  50. $OptionsDummy    = GUICtrlCreateDummy()
  51. $OptionsContext    = GUICtrlCreateContextMenu($OptionsDummy)
  52. $OptionsCommon    = GUICtrlCreateMenuItem("Common", $OptionsContext)
  53. $OptionsFile    = GUICtrlCreateMenuItem("File", $OptionsContext)
  54. GUICtrlCreateMenuItem("", $OptionsContext)
  55. $OptionsExit    = GUICtrlCreateMenuItem("Exit", $OptionsContext)
  56.  
  57.  
  58. $HelpBtn        = GUICtrlCreateButton("&Help", 90, 10, 70, 20, $BS_FLAT)
  59.  
  60. ; Create a dummy control and a contextmenu for the help too
  61. $HelpDummy        = GUICtrlCreateDummy()
  62. $HelpContext    = GUICtrlCreateContextMenu($HelpDummy)
  63. $HelpWWW        = GUICtrlCreateMenuItem("Website", $HelpContext)
  64. GUICtrlCreateMenuItem("", $HelpContext)
  65. $HelpAbout        = GUICtrlCreateMenuItem("About...", $HelpContext)
  66.  
  67.  
  68. GUISetState()
  69.  
  70. While 1
  71.     $Msg = GUIGetMsg()
  72.     
  73.     Switch $Msg
  74.         Case $OptionsExit, $GUI_EVENT_CLOSE
  75.             ExitLoop
  76.             
  77.         Case $OptionsBtn
  78.             ShowMenu($hGui, $Msg, $OptionsContext)
  79.             
  80.         Case $HelpBtn
  81.             ShowMenu($hGui, $Msg, $HelpContext)
  82.             
  83.         Case $HelpAbout
  84.             Msgbox(64, "About...", "GUICtrlGetHandle-Sample")
  85.     EndSwitch    
  86. WEnd
  87.     
  88. Exit
  89.  
  90.  
  91. ; Show a menu in a given GUI window which belongs to a given GUI ctrl
  92. Func ShowMenu($hWnd, $CtrlID, $nContextID)
  93.     Local $hMenu = GUICtrlGetHandle($nContextID)
  94.     
  95.     $arPos = ControlGetPos($hWnd, "", $CtrlID)
  96.     
  97.     Local $x = $arPos[0]
  98.     Local $y = $arPos[1] + $arPos[3]
  99.     
  100.     ClientToScreen($hWnd, $x, $y)
  101.     TrackPopupMenu($hWnd, $hMenu, $x, $y)
  102. EndFunc
  103.  
  104.  
  105. ; Convert the client (GUI) coordinates to screen (desktop) coordinates
  106. Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
  107.     Local $stPoint = DllStructCreate("int;int")
  108.     
  109.     DllStructSetData($stPoint, 1, $x)
  110.     DllStructSetData($stPoint, 2, $y)
  111.  
  112.     DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
  113.     
  114.     $x = DllStructGetData($stPoint, 1)
  115.     $y = DllStructGetData($stPoint, 2)
  116.     ; release Struct not really needed as it is a local 
  117.     $stPoint = 0
  118. EndFunc
  119.  
  120.  
  121. ; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
  122. Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
  123.     DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
  124. EndFunc